package com.zeedev.flyingsaucer.beers.activities.tabchooser;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.zeedev.flyingsaucer.beers.activities.tabchooser.impl.MemberTabModel;
import com.zeedev.flyingsaucer.beers.activities.tabchooser.impl.StoreTabModel;
import com.zeedev.flyingsaucer.beers.activities.tabchooser.interfaces.TabModel;
import com.zeedev.flyingsaucer.modules.DownloadModule.interfaces.DownloadManager;
import com.zeedev.flyingsaucer.modules.IndicatorModule.impl.IndicatorImpl;
import com.zeedev.flyingsaucer.modules.IndicatorModule.interfaces.Indicator;
import com.zeedev.flyingsaucer.util.Generate;
import mobi.zeedev.flyingsaucer.common.DataModule.BeerConstants;
import roboguice.activity.RoboTabActivity;
import roboguice.util.Ln;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

public class BeerListParent extends RoboTabActivity {

    private TabHost mTabHost;

    @Inject
    DownloadManager downloadManager;

    private TabModel tabModel;

    private ProgressDialog progress;

    @Inject
    Injector injector;

    @Inject
    Indicator indicator;

    private PropertyChangeListener mIndicatorListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        this.setProgressBarIndeterminate(true);
        this.setProgressBarVisibility(true);

        progress = Generate.createProgressDialog(this, "Downloading beers...", "There are so many beers to choose from.");
        progress.dismiss();

        mTabHost = this.getTabHost();
        String extra = this.getIntent().getStringExtra(BeerConstants.INTENT_TAB_MODEL);

        if (extra.equals("MemberTabModel")) {
            tabModel = injector.getInstance(MemberTabModel.class);
        } else if (extra.equals("StoreTabModel")) {
            tabModel = injector.getInstance(StoreTabModel.class);
        } else {
            Ln.w("Tab Model should be specified.");

        }

        if (tabModel == null) {
            Ln.e("Tab Model should not be null");
            return;
        }


        tabModel.setTabLayout(mTabHost, this);

        mIndicatorListener = new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getNewValue() == null) {
                    return;
                }
                Boolean newValue = (Boolean) evt.getNewValue();
                BeerListParent.this.setIndicatorVisible(newValue, false);

            }
        };

        indicator.addPropertyChangeListener(IndicatorImpl.Properties.INDICATOR.name(), mIndicatorListener);

        downloadManager.startRefreshIfNeeded();

    }


    //TODO is this fixed???
/*    @Override
    protected void onRestoreInstanceState(Bundle state) {
//		Log.d(TAG, "onRestoreInstanceState");
        if (state != null && state.containsKey("ufo_tab_index")) {
            this.getTabHost().setCurrentTab(state.getInt("ufo_tab_index"));

//			this.controller = (ListParentController) state.getSerializable("beer_list_parent_controller");
        }
        try {
            super.onRestoreInstanceState(state);
        } catch (Exception e) {
            //just catch this we don't care, it will still load and work fine
            //e.printStackTrace();
        }
    }*/

    //TODO may be fixed!
/*    @Override
    protected void onSaveInstanceState(Bundle outState) {
//		Log.d(TAG, "onSaveInstanceState");
        outState.putInt("ufo_tab_index", this.getTabHost().getCurrentTab());
//		this.controller.setParent(null);
//		outState.putSerializable("beer_list_parent_controller", this.controller);
        super.onSaveInstanceState(outState);
    }*/

    //Do default behaviour
/*    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {

//			controller.destroy();

            return true;
        } else {
            return false;
        }

    }*/


    private void setIndicatorVisible(Boolean visible, Boolean modal) {

        if (modal && visible) {
            progress.show();
        } else {
            progress.dismiss();
        }

        if (!modal && visible) {
            setProgressBarIndeterminateVisibility(true);
        } else {
            setProgressBarIndeterminateVisibility(false);
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        indicator.removePropertyChangeListener(mIndicatorListener);
    }


}
